package TestPaperSystem.controller;


import TestPaperSystem.Dto.*;
import TestPaperSystem.entity.Paper;
import TestPaperSystem.service.PaperService;
import TestPaperSystem.utils.R;
import TestPaperSystem.utils.ResponseUtils_R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xwj
 * @since 2023-03-25
 */
@RestController
@RequestMapping("/paper")
public class PaperController {

    @Autowired
    PaperService paperService;

    @PostMapping("/selfPaperList")
    public R selfPaperList(@RequestBody PaperVo paperVo){
        Page papers = null;
        try {
            papers = paperService.selfPaperList(paperVo);
        }catch (Exception e){
            ResponseUtils_R.success(e.getMessage());
        }

        return ResponseUtils_R.success(papers);
    }

    @PostMapping("/create-paper")
    public R createPaper(@RequestBody PaperVo paper){
        String id ;
        try{

            id = paperService.createPaper(paper);

        }catch (Exception e){
            e.printStackTrace();
            return ResponseUtils_R.error(e.getMessage());
        }
        return ResponseUtils_R.success(id);

    }

    /*
        检查当前用户是否有正在进行的考试
    */
    @GetMapping("/check-process/{userId}")
    public R checkProcess(@PathVariable String userId) throws Exception{
        return ResponseUtils_R.success(paperService.checkProcess(userId));
    }


    @PostMapping("/paper-details")
    public R paperDetails(@RequestBody PageUtils_VO page) throws Exception{
        PaperDetails paperDetails ;
        try{
            paperDetails = paperService.paperDetails(page.getId());
        }catch (Exception e){
            return ResponseUtils_R.error(e.getMessage());
        }
        return ResponseUtils_R.success(paperDetails);

    }

    @PostMapping("/paper-result")
    public R paperResult(@RequestBody PaperVo paperVo ){
        PaperResult result = null;

        try {
            result = paperService.paperResultById(paperVo);
        }catch (Exception e){
            return ResponseUtils_R.error(e.getMessage());
        }
        return ResponseUtils_R.success(result);
    }


    @PostMapping("/qu-details")
    public R quDetails(@RequestBody PaperQuVo quVo) throws Exception{
        PaperQuDTO quDTO = new PaperQuDTO();

        try{
            quDTO = paperService.quDetails(quVo);

        }catch (Exception e){

            return ResponseUtils_R.error(e.getMessage());

        }
        return ResponseUtils_R.success(quDTO);
    }

    @PostMapping("/fill-answer")
    public R fillQu(@RequestBody PaperQuVo paperQuVo)throws Exception{
        paperService.fillAnswer(paperQuVo);
        return ResponseUtils_R.success(null);
    }

    @PostMapping("/handExam")
    public R handExam(@RequestBody PaperQuVo paperQuVo) throws Exception {
        paperService.handExam(paperQuVo);
        return ResponseUtils_R.success(null);
    }


    @PostMapping("/updateState")
    public R updateState(@RequestBody PaperVo paperVo){
        boolean state = false;
        try {
            state = paperService.updateState(paperVo);
        }catch (Exception e){
            return ResponseUtils_R.error(e.getMessage());
        }
        return ResponseUtils_R.success(state);
    }

}

